﻿define([
    'require',
    'angular',
    'underscore',
    'system2/Hash'
], function (require, angular, _,Hash) {
    return function ($scope,$routeParams, $http, RuleSerivce, GillionMsg,$rootScope) {
        RuleSerivce.readyData();
        var recentlyPackageClassArr = ["package package-a","package package-b","package package-c","package package-d","package package-e","package package-f"];
        angular.element('.engine-tab li:first').trigger('click');
        $scope.toggleSearch = function(){
            $scope.showSearchPackage = !$scope.showSearchPackage;
            if ($scope.showSearchPackage) {
                angular.element('.package-list').css('top', '140px');
            } else {
                angular.element('.package-list').css('top', '100px');
            }
        };

        $scope.toggleSearch2 = function(){
            $scope.showSearchPackage = !$scope.showSearchPackage;
            if ($scope.showSearchPackage) {
                angular.element('.package-list').css('top', '205px');
            } else {
                angular.element('.package-list').css('top', '165px');
            }
        };

        if ($rootScope.currPackage && $rootScope.currPackage.projectid==$rootScope.currProjcet.id){
            angular.element('.package-list').css('top', '165px');
        }

        /**
         * 获取最近使用规则包的颜色
         * @param packageId
         * @returns {string}
         */
        $scope.getRecentlyPackageClass = function(packageId){
            var index = parseInt(Hash(packageId) % 6);
            return recentlyPackageClassArr[index];
        };
        RuleSerivce.readyData();
        $scope.projectId = $routeParams.projectId;
        $http.get('/grule-manager/rulePackage/getRulePackages/' + $scope.projectId).success(function (resp) {
            if(resp.data=="reload"){
                location.reload();
            }else {
                $scope.packages = resp.data;
            }
        });
        $http.post('/grule-manager/projects/childList/' + $scope.projectId).success(function (resp) {
            if(resp.data=="reload"){
                location.reload();
            }else {
                $scope.projects = resp.projects;
            }
        });


        $scope.selectProject = function (project) {
            document.cookie = "scrollTop=0";
            RuleSerivce.setCurrentProject(project);
        };
        $scope.selectPackage = function (pg) {
            RuleSerivce.setCurrentPackage(pg);
            RuleSerivce.setVariableScope(1);
        };
        $http.get('/grule-manager/monitor').success(function (data) {
            $scope.reports = data;
        });

        $scope.createPackage = function () {
            $scope.project={
                projectId:$scope.projectId,
                projectCode:$rootScope.currProjcet.projectCode
            }
            GillionMsg.showUrl({
                title: '新建规则包',
                url: '/html2/rule/create/createPackage',
                width: 400,
                height: 250,
                data: $scope.project,
                onClose: function (data) {
                    if (data.id) {
                        RuleSerivce.setCurrentPackage(data);
                        $rootScope.PID=1;
                        $http({
                            url:"/grule-manager/updatePermission",
                            method:"GET"
                        }).success(function(resp){
                            $rootScope.$sessionAttrs.projectPermission=resp;
                        });
                        window.location.href="../../home#/projects/"+$scope.projectId+"/packages/"+data.id;
                    }
                }
            });
        };

        $scope.copyPackage = function () {
            $scope.project={
                projectId:$scope.projectId,
                projectCode:$rootScope.currProjcet.projectCode
            }
            GillionMsg.showUrl({
                title: '复制规则包',
                url: '/html2/rule/create/copyPackage',
                width: 400,
                height: 300,
                data: $scope.project,
                onClose: function (data) {
                    if(data.success){
                        $http.get('/grule-manager/rulePackage/getRulePackages/' + $scope.projectId).success(function (resp) {
                            if(resp.data=="reload"){
                                location.reload();
                            }else {
                                $scope.packages = resp.data;
                                RuleSerivce.setCurrentPackage(data);
                                $rootScope.PID=1;
                                $http({
                                    url:"/grule-manager/updatePermission",
                                    method:"GET"
                                }).success(function(resp){
                                    $rootScope.$sessionAttrs.projectPermission=resp;
                                });
                                GillionMsg.alert('提示', '复制成功！');
                            }
                        });

                    }
                    /*if (data.id) {
                        RuleSerivce.setCurrentPackage(data);
                        $rootScope.PID=1;
                        $http({
                            url:"/grule-manager/updatePermission",
                            method:"GET"
                        }).success(function(resp){
                            $rootScope.$sessionAttrs.projectPermission=resp;
                        });
                        window.location.href="../../home#/projects/"+$scope.projectId+"/packages/"+data.id;
                    }*/
                }
            });
        };

    };
});